<?php

namespace Admin\Models\school;

use Admin\Models\BaseModel;
use Helpers\Main;
use Phalcon\Db\Adapter\Mongo\Db;

class XhschoolOpenclass extends BaseModel
{

    /**
     *
     * @var integer
     */
    public $iSOCId;

    /**
     *
     * @var string
     */
    public $iAppCode;

    /**
     *
     * @var integer
     */
    public $iSchoolId;

    /**
     *
     * @var integer
     */
    public $iOp;

    /**
     *
     * @var integer
     */
    public $bDelete;

    /**
     *
     * @var string
     */
    public $tCreateDate;

    /**
     *
     * @var string
     */
    public $iCreateUserId;

    /**
     *
     * @var string
     */
    public $tLastModifyDate;

    /**
     *
     * @var string
     */
    public $iLastModifyUserId;

    /**
     * Initialize method for model.
     */
    public function initialize()
    {
        parent::initialize();
        $this->setSource("XHSchool_OpenClass");
    }

    /**
     * Returns table name mapped in the model.
     *
     * @return string
     */
    public function getSource()
    {
        return 'XHSchool_OpenClass';
    }

    /**
     * Allows to query a set of records that match the specified conditions
     *
     * @param mixed $parameters
     * @return XhschoolOpenclass[]
     */
    public static function find($parameters = null)
    {
        return parent::find($parameters);
    }

    /**
     * Allows to query the first record that match the specified conditions
     *
     * @param mixed $parameters
     * @return XhschoolOpenclass
     */
    public static function findFirst($parameters = null)
    {
        return parent::findFirst($parameters);
    }

    /**
     * 根据开班id删除记录
     *
     * @param $openClassId
     * @return bool
     *
     * create by ck 20160929
     */
    public static function deleteRecordById($openClassId)
    {
        return self::findFirst($openClassId)->delete();
    }

    /**
     * 根据数据及op新建记录并返回
     *
     * @param $data
     * @param int $op
     * @return XhschoolOpenclass
     *
     * create by ck 20160815
     */
    public static function createRecord($data, $op = 0)
    {
        $obj = new self;
        $obj->iAppCode      = $data['iAppCode'];
        $obj->iSchoolId     = isset($data['iSchoolId']) ? $data['iSchoolId'] : 0;
        $obj->iOp           = $op;
        return $obj;
    }

    /**
     * 根据相关信息查找是否有未删除的本地开班记录，有则返回开班ID，否则返回false
     *
     * @param $schoolId
     * @param $appCode
     * @param $teacherId
     * @return bool
     *
     * create by ck 20160816
     * modify by ck 20170224    添加命名空间，方法更改
     */
    public static function getOldOpenClassId($schoolId, $appCode, $teacherId)
    {
        $manageId = Main::getLoginUserInfo('userId');
        $openClass = \Phalcon\Di::getDefault()->get('modelsManager')->createBuilder()
            ->columns('open.iSOCId')
            ->from(['open' => '\\Admin\\Models\\school\\XhschoolOpenclass'])
            ->leftJoin('\\Admin\\Models\\school\\XhschoolClassteacher', 'tch.iSOCId = open.iSOCId', 'tch')
            ->where(
                "open.iAppCode='{$appCode}' AND open.iSchoolId=:iSchoolId: AND open.bDelete=0 AND open.iCreateUserId=:iUserId: AND open.iOp=1",
                array(
                    'iSchoolId'     => $schoolId,
                    'iUserId'       => $manageId
                )
            )
            ->andWhere(
                'tch.bDelete=0 AND tch.iTchId=:iTeacherId:',
                array('iTeacherId' => $teacherId)
            )
            ->orderBy('open.tCreateDate DESC')
            ->limit(1)
            ->getQuery()
            ->execute()
            ->toArray();
        return isset($openClass[0]) ? $openClass[0]['iSOCId'] : false;
//        $openClass = XhschoolOpenclass::query()
//            ->leftJoin('XhschoolClassteacher', 'tch.iSOCId = XhschoolOpenclass.iSOCId', 'tch')
//            ->where(
//                'iAppCode=:iAppCode: AND iSchoolId=:iSchoolId: AND XhschoolOpenclass.bDelete=0 AND XhschoolOpenclass.iCreateUserId=:iUserId: AND XhschoolOpenclass.iOp=1',
//                array(
//                    'iAppCode'      => $appCode,
//                    'iSchoolId'     => $schoolId,
//                    'iUserId'       => $manageId
//                )
//            )
//            ->andWhere(
//                'tch.bDelete=0 AND tch.iTchId=:iTeacherId:',
//                array('iTeacherId' => $teacherId)
//            )
//            ->orderBy('XhschoolOpenclass.tCreateDate DESC')
//            ->execute()
//            ->getFirst();
//        return $openClass ? $openClass->iSOCId : false;
    }

}
